package MFL.dao.impl;

//import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

//import org.apache.commons.dbutils.ResultSetHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;

//import com.sun.org.apache.bcel.internal.generic.NEW;

import MFL.dao.OrderDao;
import MFL.handler.CommendResultSetHandler;
import MFL.handler.OrderResultSetHandler;
import MFL.handler.OrderResultsetRemsg;
import basedao.BaseDao;
import bean.comment;
import bean.orders;

public class OrderDaoImpl extends BaseDao implements OrderDao{

	@Override
	public Integer deleteOrderById(Integer id) {
		
		try {
			return query.update("delete from orders where id=?",id);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return -1;
	}

	@Override
	public List<orders> GetOrderPaging(Integer pageIndex, Integer pageSize) {
		// TODO Auto-generated method stub
		String sql="select or1.id,or1.receivemsgId,remsg.receiverName,remsg.receiverPhone,or1.totalprice,or1.state, \r\n" + 
				"or1.payTime,or1.payType,or1.sendTime,or1.sendType,or1.message,or1.endTime,or1.createTime,or1.updateTime\r\n" + 
				"from orders or1,receiverMsg remsg \r\n" + 
				"where remsg.id=or1.receivemsgId order by or1.id limit ?,?;";
		try {
			return query.query(sql, new OrderResultSetHandler(), (pageIndex-1)*pageSize,pageSize);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}

	@Override
	public Integer getCount() {
		String sql="select count(1) from orders";
		Object query1;
		try {
			query1 = query.query(sql, new ScalarHandler());
			return Integer.parseInt(query1.toString());
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}



//	@Override
	public Integer addorderlist(orders info) {
		// TODO Auto-generated method stub
		try {
			return query.update("insert into orders(userId,receivemsgId,sendType,message,\r\n" + 
					"payType,state,payTime,sendTime,endTime,createTime,updateTime,totalprice) values(?,?,?,?,?,?,?,?,?,?,?,?)",
					info.getUserid(),info.getReceivemsgid(),info.getSendtype(),info.getMessage(),
					info.getPaytype(),info.getState(),info.getPaytime(),info.getSendtime(),
					info.getEndtime(),info.getCreatetime(),info.getUpdatetime(),info.getTotalprice());
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}

	@Override
	public List<orders> selectOrderbyorderId(Integer id, Integer pageIndex, Integer pageSize) {
		String sql="select or1.id,or1.receivemsgId,remsg.receiverName,remsg.receiverPhone,or1.totalprice,or1.state, \r\n" + 
				"or1.payTime,or1.payType,or1.sendTime,or1.sendType,or1.message,or1.endTime,or1.createTime,or1.updateTime\r\n" + 
				"from orders or1,receiverMsg remsg \r\n" + 
				"where remsg.id=or1.receivemsgId  and or1.id=? order by or1.id limit ?,?;";
		
		try {
			return query.query(sql,new OrderResultsetRemsg(),id,(pageIndex-1)*pageSize,pageSize );
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}
	
}


